package com.gemo.demo.controller;

import com.gemo.demo.entiy.News;
import com.gemo.demo.entiy.Type;
import com.gemo.demo.service.IAdminService;
import com.gemo.demo.service.INewsService;
import com.gemo.demo.service.ITypeService;
import com.gemo.demo.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController implements Serializable {
    @Autowired
    private ITypeService typeService;
    @Autowired
    private IAdminService adminService;

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session){
        Object admin=adminService.login(username,password);
        System.out.println(admin);
        if(admin!=null){
            //已登录 状态为0
            List<Type> types=typeService.getTypes();
            session.setAttribute("types",types);
            session.setAttribute("currAdmin",admin);
            System.out.println(session.getAttribute("currAdmin"));
            session.setAttribute("status",0);
            return "redirect:/news/list";
        }else{
            //账号密码有误 状态为1
            session.setAttribute("status",1);
            return "redirect:/toLogin";
        }
    }
}

